home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / rpcinfo / RCS / getopt.c,v < prev    next >
Encoding:
Text File  |  1988-11-17  |  1.6 KB  |  102 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     88.11.16.16.54.41;  author mendel;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @Public domain source from SUN.
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* @@(#)getopt.c    1.2 87/11/30 3.9 RPCSRC */
  27.  
  28. /* this is a public domain version of getopt */
  29.  
  30. /*LINTLIBRARY*/
  31. #ifndef NULL
  32. #define NULL    0
  33. #endif NULL
  34. #ifndef EOF
  35. #define EOF    (-1)
  36. #endif EOF
  37.  
  38. #define ERR(s, c)    if(opterr){\
  39.     extern int strlen(), write();\
  40.     char errbuf[2];\
  41.     errbuf[0] = c; errbuf[1] = '\n';\
  42.     (void) write(2, argv[0], strlen(argv[0]));\
  43.     (void) write(2, s, strlen(s));\
  44.     (void) write(2, errbuf, 2);}
  45.  
  46. #define strchr index
  47.  
  48. extern int strcmp();
  49. extern char *strchr();
  50.  
  51. int    opterr = 1;
  52. int    optind = 1;
  53. int    optopt;
  54. char    *optarg;
  55.  
  56. int
  57. getopt(argc, argv, opts)
  58. int    argc;
  59. char    **argv, *opts;
  60. {
  61.     static int sp = 1;
  62.     register int c;
  63.     register char *cp;
  64.  
  65.     if(sp == 1)
  66.         if(optind >= argc ||
  67.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  68.             return(EOF);
  69.         else if(strcmp(argv[optind], "--") == NULL) {
  70.             optind++;
  71.             return(EOF);
  72.         }
  73.     optopt = c = argv[optind][sp];
  74.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  75.         ERR(": unknown option, -", c);
  76.         if(argv[optind][++sp] == '\0') {
  77.             optind++;
  78.             sp = 1;
  79.         }
  80.         return('?');
  81.     }
  82.     if(*++cp == ':') {
  83.         if(argv[optind][sp+1] != '\0')
  84.             optarg = &argv[optind++][sp+1];
  85.         else if(++optind >= argc) {
  86.             ERR(": argument missing for -", c);
  87.             sp = 1;
  88.             return('?');
  89.         } else
  90.             optarg = argv[optind++];
  91.         sp = 1;
  92.     } else {
  93.         if(argv[optind][++sp] == '\0') {
  94.             sp = 1;
  95.             optind++;
  96.         }
  97.         optarg = NULL;
  98.     }
  99.     return(c);
  100. }
  101. @
  102.